import { useEffect } from "react";
import Utils from "../utils";
import proxy from "@/network/proxy";

/**
 *重定向至发布通知页面
 * @param {*} noticeSysAddr 通知系统地址
 * @param {*} lg_ic 身份code
 * @param {*} schoolId 学校id
 */
function useRedirectNotice(noticeSysAddr, lg_ic, schoolId) {
  const isPublish = Utils.getQueryVariable("isPublish");
  const noticeType = Utils.getQueryVariable("noticeType");
  const token = Utils.getNowToken();
  const env = Utils.getSystemInfo() === "weapp";

  useEffect(() => {
    if (!isPublish || !noticeSysAddr) return;
    let url =
      noticeSysAddr +
      `/mobile/announcement/index.html?lg_tk=${token}&lg_ic=${lg_ic}&noticeType=${noticeType}`;
    if (env) {
      url = proxy.transmitUrlToVisit(
        noticeSysAddr +
          `/mobile/announcement/index.html?lg_tk=${token}&lg_ic=${lg_ic}&env=weapp&schoolId=${schoolId}&noticeType=${noticeType}`
      );
    }

    // 开发环境测试
    // if (process.env.NODE_ENV === "development") {
    //   url = `http://192.168.129.86:10087/?lg_tk=${token}&lg_ic=${lg_ic}&env=${env}&noticeType=${noticeType}`;
    // }
    // window.location.href = url;
    window.location.replace(url);

    // window.history.replaceState(null, "通知公告", url);
    // window.location.replace("");
  }, [noticeSysAddr, isPublish]);
}

export default useRedirectNotice;
